import hashlib
import random
import time

#file_path=input("请输入所要分块的文件路径1")

def cal_hash(file_content):
    # 确保输入是字节类型
    if not isinstance(file_content, bytes):
        raise TypeError("输入必须是字节类型")
    # 创建 SHA-256 对象
    sha256_hash = hashlib.sha256()
    # 用数据更新哈希对象
    sha256_hash.update(file_content)
    # 计算并返回哈希值
    return int(sha256_hash.hexdigest(),16)
# def chunk(break_points,file_path  ):
#     cnt=0
#     with open(file_path,"rb")as f:
#         for i in break_points :
#             data=f.read(i)
#             new_file_path=f"cnt{cnt}_file.bin"
#             with open(new_file_path ,"wb")as nf:
#                 nf.write(data)
#                 cnt+=1
#         # data=f.read()
#         # with open(new_file_path, "wb") as nf:
#         #
#         #     nf.write(data)
#         #     cnt += 1
class Tttd:
    def __init__(self,   d1=540,d2=270,max_t=2800,min_t=460,windows_size=256):
        self.d1=d1
        self.d2=d2
        self.max_t=max_t
        self.min_t=min_t
        self.windows_size=windows_size
    def Tttd_break(self,file_path):
        break_points = list()
        las = 0
        now=las+self.windows_size
        bu_point = 0
        with open(file_path, 'rb') as original_f:
            data = original_f.read()
            while now<len(data):
                if now-las<self.min_t :
                    now+=1
                    continue
                if cal_hash(data[now-self.windows_size :now])%self.d2 ==self.d2-1:
                    bu_point =now
                    now+=1
                if cal_hash(data[now - self.windows_size:now]) % self.d1 == self.d1 - 1:
                    break_points.append(now)
                    las=now
                    now=now+self.windows_size
                    bu_point =0
                if now -las<self.max_t :
                    now+=1
                    continue
                if bu_point :
                    break_points.append(bu_point )
                    las = bu_point
                    now = bu_point  + self.windows_size
                    bu_point = 0
                else :
                    break_points .append(now)
                    las = now
                    now = now + self.windows_size
                    bu_point = 0
            if las < len(data):
                break_points.append(len(data))
            return break_points

#def main():
   # start_time=time.time()
    #tttds=Tttds ()
   # points=tttds.Tttds_break(file_path)
    ##print(points)
    #end_time = time.time()
   ## print(f"程序运行了{end_time-start_time }秒")
 ##   print(f"一共{len(points)}块")
   # chunk(points ,file_path)



# if __name__ == "__main__":
#     main()






